package com.jwater.core.net;

import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jwater.org.springframework.cglib.proxy.Enhancer;
import jwater.org.springframework.cglib.proxy.MethodInterceptor;
import jwater.org.springframework.cglib.proxy.MethodProxy;
/**
 * @description:
 * @author: jnner
 * @createDate: 2021/2/5
 * @version: 1.0
 */
public class ObjectProxy implements MethodInterceptor {
	protected static Logger logger = LoggerFactory.getLogger(ObjectProxy.class);

	private SimpleNode node;
	private String superClazz;
	private int timeout = SimpleNode.timeout;

	public ObjectProxy(Node node) {
		this.node = (SimpleNode) node;
	}

	public Object getProxyObject(String superClazz, long timeout) throws ClassNotFoundException {
		this.superClazz = superClazz;
		this.timeout = (int) timeout;
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(Class.forName(superClazz));
		enhancer.setCallback(this);
		return enhancer.create();
	}

	@Override
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		AsyncResult asyncResult = CallUtils.getAsyncResultFromLocal();
		Integer t = CallUtils.getTimeoutFromLocal();
		t = t == null ? timeout : t;
		return node.intercept(t, superClazz, obj, method, args, proxy, asyncResult);
	}
}
